# -*- coding: gbk -*-

"""
Tag list, Message list, Thread list
"""

import web, cjson
import hall, uigate
import codecs
import pyrpc

Unicode = codecs.getdecoder('utf8')

def unicodeHeader (mesg, headerList):
    return map(lambda x: Unicode(x, 'replace')[0],
               mesg.getMailHeader(headerList))

class UIList:
    GET  = web.autodelegate('GET_')
    
    def GET_tagList (self):
        ls = uigate.getTagList()
        ls = list(ls)
        ls.sort()
        ret = map(lambda x: (x, uigate.TagView(x).numUnread()), ls)
        web.header('Content-Type', 'text/javascript')
        print cjson.encode(ret)

    def GET_tagNumUnread (self, tag):
        ret = uigate.TagView(tag[1:]).numUnread();
        print ret;

    def GET_tagMessages (self, path):
        tmp, tagName, page = path.split('/')
        page = int(page)
        
        tag = uigate.TagView(tagName)
        ls = tag.browse(page * hall.uiconf['PAGEITEMS'],
                        hall.uiconf['PAGEITEMS'])


        try:
            starStat = hall.starSt.map('getStar', map(lambda x: (x[1],), ls))
        except pyrpc.rpc.RemoteError:
            starStat = [False] * len(ls)
            
        titleStat = []
        for docID, threadID, numUnread in ls:
            doc = uigate.MessageView(docID)
            titleStat.append(list(unicodeHeader(doc, ('SUBJECT', 'FROM'))))            

        ret = map(lambda i:
                      { 'docID' : ls[i][0],
                        'threadID' : ls[i][1],
                        'isStar' : starStat[i],
                        'subject' : titleStat[i][0],
                        'from' : titleStat[i][1],
                        'numUnread' : ls[i][2]},                  
                  range(0, len(ls)))        
        
        web.header('Content-Type', 'text/javascript')
        print cjson.encode(ret)

    def GET_thread (self, threadID):
         threadID = int(threadID[1:])
         thread = uigate.ThreadView(threadID)
         
         web.header('Content-Type', 'text/javascript')
         print cjson.encode((thread.isStar(), thread.getMessages()))

    def GET_threadUnread (self, threadID):
        threadID = int(threadID[1:])
        thread = uigate.ThreadView(threadID)
        web.header('Content-Type', 'text/javascript')
        print cjson.encode((thread.isStar(), thread.getUnreadMessages()))

    def GET_starMessages (self, page):
        page = int(page[1:])
        num = hall.uiconf['PAGEITEMS']
        thList = uigate.getStarList(page * num, num)
        
        docList = []
        titleStat = []
        try:
            threadStat = hall.brCache.map('getThreadNumUnread', map(lambda x: (x,), thList))
        except:
            threadStat = [0] * len(thList)

        for threadID in thList:
            thread = uigate.ThreadView(threadID)
	    threadDocList = thread.getMessages()
	    if (len(threadDocList) == 0):
		continue
	    docID = threadDocList[-1]
            docList.append(docID)
            doc = uigate.MessageView(docID)
            titleStat.append(unicodeHeader(doc, ('SUBJECT', 'FROM')))

        ret = map(lambda i:
                      { 'docID' : docList[i],
                        'threadID' : thList[i],
                        'isStar' : True,
                        'subject' : titleStat[i][0],
                        'from' : titleStat[i][1],
                        'numUnread' : threadStat[i]},
                  range(0, len(thList)))
        web.header('Content-Type', 'text/javascript')
        print cjson.encode(ret)

    def GET_searchMessages (self, page):
        page = int(page[1:])    
        searchView = uigate.SearchView(web.input()['query'])        
        ls = searchView.browse(page * hall.uiconf['PAGEITEMS'],
                               hall.uiconf['PAGEITEMS'])
        
        try:
            starStat = hall.starSt.map('getStar', map(lambda x: (x[1],), ls))
        except pyrpc.rpc.RemoteError:
            starStat = [False] * len(ls)
            
        titleStat = []
        for docID, threadID, numUnread in ls:
            doc = uigate.MessageView(docID)
            titleStat.append(list(unicodeHeader(doc, ('SUBJECT', 'FROM'))))            

        ret = map(lambda i:
                      { 'docID' : ls[i][0],
                        'threadID' : ls[i][1],
                        'isStar' : starStat[i],
                        'subject' : titleStat[i][0],
                        'from' : titleStat[i][1],
                        'numUnread' : ls[i][2]},                  
                  range(0, len(ls)))        
        
        web.header('Content-Type', 'text/javascript')
        print cjson.encode(ret)
        
        
        

        

